﻿namespace Cubezicles.UI
{
    using System.Collections.Generic;
    using Cubezicles.NeedObjects;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    public class ItemManagerPane : ManagerPane
    {
        private ItemManager im;

        // Textures
        private new Texture2D butts;

        public ItemManagerPane(Game1 game, ItemManager itemMan) : base(game)
        {
            im = itemMan;

            int xPos = 75;
            int yPos = 75;
            int w = game.screenW/2 - 100;
            int h = 430;

            PanePosition = new Vector2(xPos, yPos);
            PaneSize = new Vector2(w, h);
            FrameTitle = game.Messages["ItemPaneTitle"];
        }

        protected override void LoadContent()
        {
            butts = Game.Content.Load<Texture2D>("UI/buttons");

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            if (IsVisible)
            {
                // Create a list of UI controls
                controls = new List<Button>();

                // Get the screen width and height
                int screenH = GraphicsDevice.Viewport.Height;
                int screenW = GraphicsDevice.Viewport.Width;

                game.SpriteBatch.Begin();

                // Draw the frame that hold the info
                DrawFrame();
                //game.drawFrame(75, 75, screenW / 2 - 100, 430, game.Messages["ItemPaneTitle"], game.Fonts["status"]);

                // List all the items
                int y = 105;
                int x = 85;
                const int lineHeight = 90; 

                DrawItemListing(im.pinball, x, y);
                y += lineHeight;
                DrawItemListing(im.drink, x, y);
                y += lineHeight;
                DrawItemListing(im.vend, x, y);
                y += lineHeight;
                DrawItemListing(im.bath, x, y);
                y += lineHeight;

                // Draw the okay button
                int xpos = x + (screenW/2 - 120)/2 - 20;
                int ypos = y;
                DrawOKbutton(xpos, ypos); // in base class

                // Draw ALL the buttons
                controls.ForEach(control => control.Update(GraphicsDevice.Viewport, game.ms, game.oldms));
                controls.ForEach(control => control.Draw(game.SpriteBatch, GraphicsDevice.Viewport));

                game.SpriteBatch.End();
            }
        }

        private void DrawBuyButton(int x, int y, int screenW, bool buyable, NeedObject item)
        {
            // Draw the Buy button
            controls.Add(new Button()
            {
                Texture = butts,
                font = game.Fonts["info"],
                text = "Buy!",
                xpos = x + screenW / 2 - 130 - 60,
                ypos = y + 50,
                Active = buyable,
                OnClick = (cont) =>
                {
                    // Check if can afford item
                    if (game.MoneyManager.Pay(item.price))
                    {
                        im.AddItem(item);
                    }
                }
            });
        }

        private void DrawItemListing(NeedObject item, int x, int y)
        {
            // Get the screen width and height
            int screenW = GraphicsDevice.Viewport.Width;
            bool buyable = true;
            // Draw listing
            // Draw the frame that hold the info
            game.drawFrame(x, y, screenW / 2 - 120, 80, item.name, game.Fonts["tiny"]);

            
            // Buy button
            if (im.HasItem(item) || game.MoneyManager.CurrentFunds < item.price) //Check to see if bought already
            {
                buyable = false;

                // Display why the player can't buy the item.

                // If there were not sufficient funds, display a notification to the user //if (game.MoneyManager.CurrentFunds < MoneyManager.CostHireBonus)
                string err = "Can't afford!";
                Color errColor = Color.Red; 

                if (im.HasItem(item))
                {
                    // Player has already bought the item
                    err = "Already have!";
                    errColor = Color.Gray;
                }
                
                // location to draw reason, should be right next to buy button
                int reasonErrorX = x + screenW/2 - 130 - 60 - (int)game.Fonts["info"].MeasureString(err).X;
                int reasonErrorY = y + 55;

                game.SpriteBatch.DrawString(game.Fonts["info"], err, new Vector2(reasonErrorX, reasonErrorY), errColor); 
            }

            //--- Draw the Buy button ---//
            DrawBuyButton(x, y, screenW, buyable, item);

            SpriteFont font = game.Fonts["tiny"];
            Color color = game.UIblue; //text color

            //--- Draw picture representation ---//
            // There's probably a better way to do this, but for now, quick and dirty method.
            int z = 55; //....???
            // Draw avatar image
            game.SpriteBatch.Draw(item.avatar, new Rectangle(x + 10, y + 25, z, z), new Rectangle(0, 0, item.avatar.Height, item.avatar.Height), Color.White);

            // x, y position to write tagline
            int tagX = x + screenW / 2 - 130 - (int)font.MeasureString(item.tagline).X;
            int tagY = y + 23;

            // Write Tagline to screen
            game.SpriteBatch.DrawString(font, item.tagline, new Vector2(tagX, tagY), color);

            string priceStr = "$" + item.price;
            // x, y position to write price
            int pX = x + screenW / 2 - 130 - (int)font.MeasureString(priceStr).X;
            int pY = y + 38;

            // Write Price to screen
            game.SpriteBatch.DrawString(font, priceStr, new Vector2(pX, pY), color);
        }
    }
}